package com.ajk.server.service.impl;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ajk.server.controller.events.CreditsEvent;
import com.ajk.server.dao.FriendsMapper;
import com.ajk.server.model.CreditsCategory;
import com.ajk.server.model.FriendPatientEventDay;
import com.ajk.server.model.Friends;
import com.ajk.server.model.query.FriendsQuery;
import com.ajk.server.model.response.FriendRes;
import com.ajk.server.service.FriendPatientEventDayService;
import com.ajk.server.service.FriendsService;
import com.ajk.server.utils.FileStoreUtil;
import com.ajk.server.utils.SpringContextUtil;

/**
 * 亲友表  <br>
 * t_friends <br>
 * v1.0 2016-05-08
 */
 @Service
 public class FriendsServiceImpl implements FriendsService {
	
	private static final Logger logger = LoggerFactory.getLogger(FriendsServiceImpl.class);
	
	@Autowired
	private FriendsMapper friendsMapper;
	
	@Autowired
	private FriendPatientEventDayService friendPatientEventDayService;
	
	/**
 	*根据ID删除 亲友表
 	*/
 	@Override
 	public Integer deleteFriendsById(Integer id){
 		return friendsMapper.deleteById(id);
 	}
 	
 	/**
 	*新增 亲友表
 	*/
 	@Override
 	@Transactional
 	public Integer batchInsertFriends(List<? extends Friends> friendss){
		Integer count = friendsMapper.batchInsert(friendss);
		return count;
 	}
 	/**
 	*新增 亲友表
 	*
 	*
 	*/
 	@Override
 	@Transactional
 	public Integer insertFriends(Friends domain){
 		//insert ignore into
		Integer count = friendsMapper.insert(domain);
		if(count<=0){
			return count;
		}
		Integer id = domain.getId();
		//添加积分
		CreditsEvent event = new CreditsEvent(id, domain.getUserId(), CreditsCategory.FRIEND_ADD);
		SpringContextUtil.getApplicationContext().publishEvent(event);
		
		return id;
 	}

 	/**
 	*更新 亲友表
 	*/
 	@Override
 	@Transactional
 	public Integer updateFriends(Friends domain){
		Integer count = friendsMapper.update(domain);
		return count;
 	}
 	
 	/**
 	*根据ID查询 亲友表
 	*/
 	public Friends findFriendsById(Integer id){
 		FriendRes result = friendsMapper.findById(id);
 		if(result!=null){
 			result.dealWith();
 		}
 		FileStoreUtil.dealWithAvatar(result);
 		Map<String, FriendPatientEventDay> friendLastPatientEvent = friendPatientEventDayService.findMyLastPatient(id);
 		
 		result.setLastPatientEvent(friendLastPatientEvent);
 		return result;
 	}

	/**
 	*通过query 查询 亲友表
 	*/
	public List<Friends> findFriendsByQuery(FriendsQuery query){
		query.dealWith();
		List<Friends> result = friendsMapper.findByQuery(query);
		if(CollectionUtils.isNotEmpty(result)) {
			Collections.sort(result, new Comparator<Friends>() {

				@Override
				public int compare(Friends o1, Friends o2) {
					if(o1!=null && o2!=null && o1.getId()!=null && o2.getId()!=null){
						return o2.getId().compareTo(o1.getId());
					}
					return 0;
				}
			});
			for (Friends friends : result) {
				friends.dealWith();
				FileStoreUtil.dealWithAvatar(friends);
			}
		}
		return result;
	}	
	
	/**
 	*通过query count总条数
 	*/
 	public Integer countFriendsByQuery(FriendsQuery query){
 		query.dealWith();
 		Integer result = friendsMapper.countByQuery(query);
 		return result;
 	}
 	
 	/**
 	* 通过ids查询
 	*/
 	public List<Friends> findFriendsByIds(List<Integer> ids){
 		List<Friends> result = friendsMapper.findByIds(ids);
		if(CollectionUtils.isNotEmpty(result)) {
			for (Friends friends : result) {
				friends.dealWith();
			}
		}
		return result;
 	}
 	
}	